package com.hjl.dao.impl;

import com.hjl.dao.DocumentDao;
import com.hjl.domain.Doc_User_inf;
import com.hjl.domain.Document_inf;
import com.hjl.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 文件数据访问层
 *
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/8 13:57
 */
public class DocumentDaoImpl implements DocumentDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 查找总页数
     *
     * @param condition 状态
     * @return int
     * @author Lenovo
     * @date 2021/10/8 14:05
     */
    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        String sql = "select count(id) from document_inf where 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        Set<String> keySet = condition.keySet();
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and ").append(key).append(" like ? ");
                params.add("%" + value + "%");
            }
        }
        return template.queryForObject(sb.toString(), Integer.class, params.toArray());
    }

    /**
     * 查找当前页的数据集合
     *
     * @param start     起始页
     * @param rows      每页记录数
     * @param condition 状态
     * @return java.util.List<com.hjl.domain.Doc_User_inf>
     * @author Lenovo
     * @date 2021/10/8 14:01
     */
    @Override
    public List<Doc_User_inf> findByPage(int start, int rows, Map<String, String[]> condition) {
        String sql = "select * from\n" +
                "            (select d.*,u.username  from document_inf d left join user_inf u on d.USERID=u.ID) a\n" +
                "where 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        Set<String> keySet = condition.keySet();
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and ").append(key).append(" like ? ");
                params.add("%" + value + "%");
            }
        }
        sb.append(" limit ? , ?");
        params.add(start);
        params.add(rows);
        System.out.println(sb.toString());
        System.out.println(params);
        return template.query(sb.toString(), new BeanPropertyRowMapper<>(Doc_User_inf.class), params.toArray());
    }

    /**
     * 删除
     *
     * @param id ID
     * @author Lenovo
     * @date 2021/10/8 14:05
     */
    @Override
    public void delDocument(int id) {
        String sql = "delete from document_inf where ID = ?";
        template.update(sql, id);
    }

    /**
     * 通过id查找（用于编辑）
     *
     * @param id ID
     * @return com.hjl.domain.Document_inf
     * @author Lenovo
     * @date 2021/10/8 14:06
     */
    @Override
    public Document_inf findDocumentById(int id) {
        String sql = "select * from document_inf where id = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Document_inf.class), id);
    }

    /**
     * 更新
     *
     * @param document 文件对象
     * @author Lenovo
     * @date 2021/10/8 14:06
     */
    @Override
    public void updateDocument(Document_inf document) {
        String sql = "update document_inf set title=?,filename=?,temporaryfilename=?,filetype=?,filebytes=?,remark=?,createdate=?,path=? WHERE ID=?";
        template.update(sql,
                document.getTitle(),
                document.getFilename(),
                document.getTemporaryfilename(),
                document.getFiletype(),
                document.getFilebytes(),
                document.getRemark(),
                document.getCreatedate(),
                document.getPath(),
                document.getId());
    }

    /**
     * 添加
     *
     * @param document 文件对象
     * @author Lenovo
     * @date 2021/10/8 14:07
     */
    @Override
    public void addDocument(Document_inf document) {
        String sql = "insert into  document_inf values(null,?,?,?,?,?,?,now(),?,?)";
        template.update(sql,
                document.getTitle(),
                document.getFilename(),
                document.getTemporaryfilename(),
                document.getFiletype(),
                document.getFilebytes(),
                document.getRemark(),
                document.getUserid(),
                document.getPath());
    }

    /**
     * 更新（不更新文件）
     *
     * @param document 文件对象
     * @author Lenovo
     * @date 2021/10/8 14:08
     */
    @Override
    public void updateWithoutDocument(Document_inf document) {
        String sql = "update document_inf set title=?,remark=?,createdate=? WHERE ID=?";
        template.update(sql,
                document.getTitle(),
                document.getRemark(),
                document.getCreatedate(),
                document.getId());
    }
}
